package com.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manage.mapper.ShoppingsListMapper;
import com.manage.mapper.ShoppingsMapper;
import com.manage.pojo.Shoppings;
import com.manage.pojo.operation.ShoppingsList;
import com.manage.utils.PagedResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Service
public class ShoppingsServiceImpl implements ShoppingsService{

    @Autowired
    private ShoppingsMapper shoppingsMapper;
    @Autowired
    private ShoppingsListMapper shoppingsListMapper;
    @Autowired
    private Sid pid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryShoppingsIsExit(String shoppingId) {
        Shoppings shopping = new Shoppings();
        shopping.setShoppingId(shoppingId);
        Shoppings result = shoppingsMapper.selectOne(shopping);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void saveShoppings(Shoppings shopping) {
        String shoppingId = pid.nextShort();
        shopping.setShoppingId(shoppingId);
        shoppingsMapper.insert(shopping);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void shoppingDelete(String shoppingId) {
        Example shoppingExample = new Example(Shoppings.class);
        Criteria criteria = shoppingExample.createCriteria();
        criteria.andEqualTo("shoppingId", shoppingId);
        shoppingsMapper.deleteByExample(shoppingExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ShoppingsList queryShoppingsInfoByShoppingId(String shoppingId) {
        ShoppingsList result = shoppingsListMapper.queryShoppingByShoppingId(shoppingId);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryAllShoppings(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<ShoppingsList> list = shoppingsListMapper.queryAllShoppings();
        PageInfo<ShoppingsList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryShoppingsInfoByUserId(Integer page, Integer pageSize, String userId) {
        PageHelper.startPage(page, pageSize);
        List<ShoppingsList> list = shoppingsListMapper.queryShoppingsByUserId(userId);
        PageInfo<ShoppingsList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryShoppingsInfoByShoesId(Integer page, Integer pageSize, String shoesId) {
        PageHelper.startPage(page, pageSize);
        List<ShoppingsList> list = shoppingsListMapper.queryShoppingsByShoesId(shoesId);
        PageInfo<ShoppingsList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }
}
